<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content" >
        <a-table :columns="columns" :scroll="{ y: scrollY }"  rowKey="f_id" :data-source="tempResultData" @expand="onExpand" :loading="loading" expandRowByClick="true" :pagination="false" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { computed, onMounted, ref } from 'vue';
  import { getSupervisionFG, getSupervisionInfo, getDeptInfo, getUserPrefix, getDeptPrefix } from '/@/api/permission/userDeptTree';
  import { BasicTable, useTable, BasicColumn } from '/@/components/Table';
  defineOptions({
    name: 'permission-organize',
  });

  // 静态数据
  const columns: BasicColumn[] = [
    {
      title: '名称',
      dataIndex: 'name',
    },
  ];
  const tempResultData = ref([]);
  const loading = ref(false); // 是否加载中
  let fgList = null,
    ryxxList = null,
    bmxxList = null;
    const  scrollY = ref('500px') // 初始化垂直滚动高度为500px
  onMounted(() => {
    window.addEventListener('resize', handleResize);
    handleResize(); // 组件挂载时立即执行一次
    initData();
  });
  function handleResize() {
      // 根据窗口高度设置 scrollY 的值
      scrollY.value = window.innerHeight - 180 + 'px'; // 假设留出100px其他UI空间
    }
  // 懒加载数据函数
  async function fetchData(currentData) {
    // 异步加载数据
    let arr1 = [];
    let arr2 = [];
    console.log('currentData.f_id', currentData.f_id);
    if (currentData.f_real_name) {
      let fgryRes = await getUserPrefix({ ryid: currentData.f_id });
      if (fgryRes.code == 200) {
        let data = fgryRes.data;
        for (let item of data) {
          if (item.gxry) {
            let gxryIdList = JSON.parse(item.gxry);
            arr1 = await getUserTreeData(ryxxList, gxryIdList);
          }
          if (item.gxbm) {
            let gxbmIdList = JSON.parse(item.gxbm);
            arr2 = await getDeptTreeData(bmxxList, gxbmIdList);
          }
          // item.key = item.f_id
        }
      }
      const childrenData = arr2.concat(arr1);
      currentData.children = childrenData;
    } else {
      let fgbmRes = await getDeptPrefix({ bmid: currentData.f_id });
      if (fgbmRes.code == 200) {
        let deptData = fgbmRes.data;
        let deptChildren1 = []
        let deptChildren2 = []
        for (let item of deptData) {
          item.name = item.f_real_name;
  
          if (item.gxry || item.gxbm) {
            item.children = []
          } else {
            item.children = null;
          }
        }

        currentData.children = deptData;
      }
    }
    loading.value = false;
    console.log('展开后数据tempResultData.value', tempResultData.value);
  }

  function onExpand(expanded, record) {
    console.log('点击展开 ==>record', record, expanded);
    if (expanded) {
      if ((record.children && record.children.length == 0) || !record.children) {
        loading.value = true;
        fetchData(record);
      }
    }
    console.log('展开后数据tempResultData.value', tempResultData.value);
  }

  async function initData(isInit = false) {
    loading.value = true;
    let ryfgRes = await getSupervisionFG();
    if (ryfgRes.code == 200) fgList = ryfgRes.data;
    console.log('res--人员分管->>>>>>', ryfgRes);
    let ryRes = await getSupervisionInfo();
    console.log('res--人员信息->>>>>>', ryRes);
    if (ryRes.code == 200) ryxxList = ryRes.data;
    let bmRes = await getDeptInfo();
    if (bmRes.code == 200) bmxxList = bmRes.data;
    console.log('res--部门信息->>>>>>', bmxxList);
    loading.value = false;
    let firstList = [];
    let treeDate = [];
    fgList.map(fgItem => {
      if (fgItem.sfdc == 1) {
        firstList.push(fgItem);
      }
    });
    // 第一层级
    let tempList = firstList.map(fgItem => {
      fgItem.name = fgItem.f_real_name;
      if (fgItem.gxry || fgItem.gxbm) {
        fgItem.children = [];
      }
      return fgItem;
    });
    tempResultData.value = tempList;
    console.log(' tempResultData.value', tempResultData.value);
  }

  async function getUserTreeData(ryxxList, gxryIdList) {
    let arr = [];
    for (const item of ryxxList) {
      for (const itemId of gxryIdList) {
        if (itemId == item.f_id) {
          console.log('getUserTreeData', item);
          arr.push({
            // key: itemId,
            name: item.f_real_name,
            ...item,
            children: await isHavdChildren(itemId),
          });
        }
      }
    }
    return arr;
  }

  async function getDeptTreeData(bmxxList, gxbmIdList) {
    let arr = [];
    for (const item of bmxxList) {
      for (const itemId of gxbmIdList) {
        if (itemId == item.f_id) {
          console.log('getUserTreeData', item);
          arr.push({
            // key: itemId,
            name: item.f_full_name,
            ...item,
            children: await isHavdDeptChildren(itemId),
          });
        }
      }
    }
    return arr;
  }

  function isHavdChildren(ryid) {
    return new Promise(async (resolve, reject) => {
      let res = await getUserPrefix({ ryid });
      let havdChild = null;
      if (res.data && res.data.length) {
        havdChild = [];
      }
      resolve(havdChild);
    });
  }
  function isHavdDeptChildren(bmid) {
    return new Promise(async (resolve, reject) => {
      let res = await await getDeptPrefix({ bmid });
      let havdChild = null;
      if (res.data && res.data.length) {
        havdChild = [];
      }
      resolve(havdChild);
    });
  }
</script>
